// https://learn.microsoft.com/en-us/typography/opentype/spec/head
// This table gives global information about the font.
// The bounding box values should be computed using only glyphs that have contours.
// Glyphs with no contours should be ignored for the purposes of these calculations.

use super::*;

#[derive(Debug, Clone)]
pub struct Table {
	pub major_version: u16,
	pub minor_version: u16,
	/// Set by font manufacturer.
	pub font_revision: Fixed,
	/// To compute: set it to 0, sum the entire font as uint32, then store 0xB1B0AFBA - sum.
	/// If the font is used as a component in a font collection file, the value of this field will be invalidated by changes to the file structure and font table directory, and must be ignored.
	pub checksum_adjustment: u32,
	/// Set to 0x5F0F3CF5.
	pub magic_number: u32,
	/// Bit 0: Baseline for font at y=0.
	/// Bit 1: Left sidebearing point at x=0 (relevant only for TrueType rasterizers) — see additional information below regarding variable fonts.
	/// Bit 2: Instructions may depend on point size.
	/// Bit 3: Force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear. It is strongly recommended that this be set in hinted fonts.
	/// Bit 4: Instructions may alter advance width (the advance widths might not scale linearly).
	/// Bit 5: This bit is not used in OpenType, and should not be set in order to ensure compatible behavior on all platforms. If set, it may result in different behavior for vertical layout in some platforms. (See Apple’s specification for details regarding behavior in Apple platforms.)
	/// Bits 6 – 10: These bits are not used in OpenType and should always be cleared. (See Apple’s specification for details regarding legacy use in Apple platforms.)
	/// Bit 11: Font data is “lossless” as a result of having been subjected to optimizing transformation and/or compression (such as compression mechanisms defined by ISO/IEC 14496-18, MicroType® Express, WOFF 2.0, or similar) where the original font functionality and features are retained but the binary compatibility between input and output font files is not guaranteed. As a result of the applied transform, the DSIG table may also be invalidated.
	/// Bit 12: Font converted (produce compatible metrics).
	/// Bit 13: Font optimized for ClearType®. Note, fonts that rely on embedded bitmaps (EBDT) for rendering should not be considered optimized for ClearType, and therefore should keep this bit cleared.
	/// Bit 14: Last Resort font. If set, indicates that the glyphs encoded in the 'cmap' subtables are simply generic symbolic representations of code point ranges and do not truly represent support for those code points. If unset, indicates that the glyphs encoded in the 'cmap' subtables represent proper support for those code points.
	/// Bit 15: Reserved, set to 0.
	pub flags: u16,
	/// Set to a value from 16 to 16384.
	/// Any value in this range is valid.
	/// In fonts that have TrueType outlines, a power of 2 is recommended as this allows performance optimization in some rasterizers.
	pub units_per_em: u16,
	/// Number of seconds since 12:00 midnight that started January 1st, 1904, in GMT/UTC time zone.
	pub created: LongDateTime,
	/// Number of seconds since 12:00 midnight that started January 1st, 1904, in GMT/UTC time zone.
	pub modified: LongDateTime,
	/// Minimum x coordinate across all glyph bounding boxes.
	pub x_min: i16,
	/// Minimum y coordinate across all glyph bounding boxes.
	pub y_min: i16,
	/// Maximum x coordinate across all glyph bounding boxes.
	pub x_max: i16,
	/// Maximum y coordinate across all glyph bounding boxes.
	pub y_max: i16,
	/// Bit 0: Bold (if set to 1);
	/// Bit 1: Italic (if set to 1)
	/// Bit 2: Underline (if set to 1)
	/// Bit 3: Outline (if set to 1)
	/// Bit 4: Shadow (if set to 1)
	/// Bit 5: Condensed (if set to 1)
	/// Bit 6: Extended (if set to 1)
	/// Bits 7 – 15: Reserved (set to 0).
	pub mac_style: u16,
	/// Smallest readable size in pixels.
	pub lowest_rec_ppem: u16,
	/// Deprecated (Set to 2).
	pub font_direction_hint: i16,
	/// 0 for short offsets (Offset16), 1 for long (Offset32).
	pub loca_format: IndexToLocationFormat,
	/// 0 for current format.
	pub glyph_data_format: i16,
}
impl Table {
	pub fn parse(data: &[u8]) -> Option<Self> {
		if data.len() < 54 {
			return None;
		}
		let mut s = Stream::new(data);
		let this = Self {
			major_version: s.read::<u16>()?,
			minor_version: s.read::<u16>()?,
			font_revision: s.read::<Fixed>()?,
			checksum_adjustment: s.read::<u32>()?,
			magic_number: s.read::<u32>()?,
			flags: s.read::<u16>()?,
			units_per_em: s.read::<u16>()?,
			created: s.read::<LongDateTime>()?,
			modified: s.read::<LongDateTime>()?,
			x_min: s.read::<i16>()?,
			y_min: s.read::<i16>()?,
			x_max: s.read::<i16>()?,
			y_max: s.read::<i16>()?,
			mac_style: s.read::<u16>()?,
			lowest_rec_ppem: s.read::<u16>()?,
			font_direction_hint: s.read::<i16>()?,
			loca_format: {
				let index_to_loc_format = s.read::<i16>()?;
				match index_to_loc_format {
					0 => IndexToLocationFormat::Short,
					1 => IndexToLocationFormat::Long,
					_ => return None,
				}
			},
			glyph_data_format: s.read::<i16>()?,
		};

		if !(16..=16384).contains(&this.units_per_em) {
			return None;
		}

		Some(this)
	}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum IndexToLocationFormat {
	Short,
	Long,
}
